<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>今日结果</title>
</head>
<body>
	<%
		String[] memberEmailList= {"754877601@qq.com",};
			     String[] memberNameList = {"杨东阳","陈世达","陈蕾","彭浩","罗枭","孙明","高广英","王广彬","路桂康","张璐"};
			     
			     String TODAY_ORDER ="订餐人均价格：";
			     String TODAY_THING = "今日点单：";
			     String THING_ELSE_ORDER = "额外物品:";
			     String THING_ELSE ="今日额外点单：";
			     String ENTER = "<br>";
		 String context = "";
		 String bill = "";
			     
			     String[] orderUsals = request.getParameterValues("orderUsal");
			     String[] orderElses = request.getParameterValues("orderElse");
			     
			     String foodPriceStr = request.getParameter("foodPrice");
			     String priceElseStr = request.getParameter("priceElse");
			     
			     String foodDesc = new String(request.getParameter("extPrice").getBytes("iso-8859-1"),"utf-8");
			     String elseDesc = new String(request.getParameter("extPriceElse").getBytes("iso-8859-1"),"utf-8");
			     
			     double foodPrice = 0;
			     double priceElse = 0;
			     if(null != foodPriceStr)
			    	 foodPrice = (Double.parseDouble(foodPriceStr))/(orderUsals.length);
			     context +="<h1>";
			     context += TODAY_ORDER;
			     context += "</h1>";
			     context += ENTER;
			     if(null != orderUsals )
			     {
			    	 context += "<ol>";
		     for(String orderUsal : orderUsals)
		     {
		    	 context += "<li>";
		    	 int index = Integer.parseInt(orderUsal);
		    	 context += (memberNameList[index] + " ： " +foodPrice);
		    	 context += ENTER;
		    	 context += "</li>";
		    	 bill += (index + ":" + foodPrice + "|");
		     }
		     context += "</ol>";
			     }
			     
			     context += (TODAY_THING + foodDesc);
			     context += ENTER;
			     
			     
			     if(null != priceElseStr&&null != orderElses)
			    	 priceElse = (Double.parseDouble(priceElseStr))/(orderElses.length);
			     {
			    	 context += ENTER;
		     if(null != orderElses){
		    	 context += "<h1>";
		    	 context += THING_ELSE_ORDER;
		    	 context += "</h1>";
		    	 context += ENTER;
		    	 context += "<ol>";
			     for(String orderElse : orderElses)
			     {
			    	 context += "<li>";
			    	 int index = Integer.parseInt(orderElse);
			    	 context += (memberNameList[index] + " : " + priceElse);
			    	 context += "</li>";
			    	 context += ENTER;
			    	 
			    	 bill += (index+ ":" + priceElse + "|");
			     }
			     context += "</ol>";
			     context += (THING_ELSE + elseDesc);
		     }
		     
			     }
			     bill +="\r\n";
			     out.println(context);
			     writeBill(bill);
			     sendEmail("754877601@qq.com",context);
	%>

	<%!void sendEmail(String to, String context) {
		String from = "15605192105@163.com";

		Properties properties = System.getProperties();
		properties.put("mail.smtp.host", "smtp.163.com");
		properties.setProperty("mail.user", "15605192105@163.com");
		properties.setProperty("mail.password", "!QAZ4rfv&UJM");
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.transport.protocol", "smtp");

		Session mailSession = Session.getDefaultInstance(properties);

		try {
			// 创建一个默认的MimeMessage对象。
			MimeMessage message = new MimeMessage(mailSession);
			// 设置 From: 头部的header字段
			message.setFrom(new InternetAddress(from));
			// 设置 To: 头部的header字段
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					to));
			// 设置 Subject: header字段
			message.setSubject("今日订餐明细");

			// 设置 HTML消息
			message.setContent(context, "text/html;charset=utf-8");
			// 发送消息
			Transport transport = mailSession.getTransport();
			transport.connect("15605192105@163.com", "!QAZ4rfv&UJM");

			transport.sendMessage(message, message.getAllRecipients());

			transport.close();
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}
	}%>

	<%!void writeBill(String bill) {
		String fileName = "bill";
		File file = new File("D:", fileName);
		byte[] bt = bill.getBytes();
		try {
			FileOutputStream in = new FileOutputStream(file, true);
			try {
				in.write(bt, 0, bt.length);
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}%>

</body>
</html>